<?php

/**
 * This is the model class for table "channels".
 *
 * The followings are the available columns in table 'channels':
 * @property string $id
 * @property string $name
 * @property string $site_name
 * @property string $url
 * @property string $stream_key
 * @property string $object_id
 * @property integer $type
 * @property integer $status
 * @property string $reason
 * @property string $created
 */
class Channels extends JLActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Channels the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'channels';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('type, status', 'numerical', 'integerOnly'=>true),
			array('id, object_id', 'length', 'max'=>16),
			array('name, site_name, url, stream_key, reason, created', 'length', 'max'=>255),
			array('url','url'),
			array('name', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => Yii::t("message","Please enter valid channel name (without spaces and special characters)")),
			array('name','checkUnique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, site_name, url, stream_key, object_id, type, status, reason, created', 'safe', 'on'=>'search'),
		);
	}
	public function checkUnique($attribute,$params){
		$object_id = null;
		$reason = null;
		// dump($this->$attribute,false);
		// dump($params);
		if(Yii::app()->user->isGuest){
			if(!empty(Yii::app()->session['object_id'])){
				$reason = Yii::app()->session['object_id'];
				$channel = Channels::model()->findByAttributes(array(
					'reason'=>$reason,
					'name'=>$this->$attribute
				));
				
				if(!empty($channel)){
					$this->addError('name', 'Name "'.$channel->name.'" has already been taken.');
				}
				
			}else{
				
			}
		}else{
			$object_id = Yii::app()->user->id;
			$channel = Channels::model()->findByAttributes(array(
				'object_id'=>$object_id,
				'name'=>$this->$attribute
			));
			if(!empty($channel)){
				$this->addError('name', 'Name "'.$channel->name.'" has already been taken.');
			}
		}
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'site_name' => 'Enter logo url small size (Optional, used as stream logo)',
			'url' => 'Url',
			'stream_key' => 'Stream Key',
			'object_id' => 'Object',
			'type' => 'Type',
			'status' => 'Status',
			'reason' => 'Reason',
			'created' => 'Created',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('site_name',$this->site_name,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('stream_key',$this->stream_key,true);
		$criteria->compare('object_id',$this->object_id,true);
		$criteria->compare('type',$this->type);
		$criteria->compare('status',$this->status);
		$criteria->compare('reason',$this->reason,true);
		$criteria->compare('created',$this->created,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getRtmp(){
		$criteria = new CDbCriteria;
		$criteria->condition = " alias LIKE :alias";
		$criteria->params = array (	
			':alias' => '%rtmp%',
		);
		$rtmp = Settings::model()->find($criteria);
		return $rtmp;
	}
}